package com.recursivechaos.gamemaster.dao;

import org.hibernate.SQLQuery;

import com.recursivechaos.gamemaster.exception.GameException;

public class CampaignDAO extends DAO {

	/**
	 * Resets all PC's health
	 * 
	 * @throws GameException
	 */
	public void partyRest() throws GameException {
		try {
			begin();
			String sql = "UPDATE recursi3_igm_db.charobjs c "
					+ "JOIN recursi3_igm_db.raceobjs r "
					+ "SET c.CHAR_CUR_HP=r.RACE_HP "
					+ "WHERE c.CHAR_TYPE='player' "
					+ "AND c.CHAR_RACE = r.RACE_NAME; ";
			SQLQuery sqlQuery = getSession().createSQLQuery(sql);
			sqlQuery.executeUpdate();

			commit();

		} catch (Exception e) {
			throw new GameException("Was not able to run party rest update.");
		} finally {
			close();
		}

	}

	/**
	 * revives all PC's by setting them conscious
	 * 
	 * @throws GameException
	 */
	public void partyRevive() throws GameException {
		try {
			begin();
			String sql = "UPDATE recursi3_igm_db.charobjs c "
					+ "SET c.CHAR_CONS='1' " + "WHERE c.CHAR_TYPE='player';";
			SQLQuery sqlQuery = getSession().createSQLQuery(sql);
			sqlQuery.executeUpdate();
			commit();
		} catch (Exception e) {
			throw new GameException("Was not able to revive party.");
		} finally {
			close();
		}

	}
}
